package bf.program.command.ext;

import org.objectweb.asm.MethodVisitor;

import bf.program.Command;
import bf.program.State;

public class CellAdd extends Command {

	int value;
	
	public CellAdd(int value) {
		super();
		this.value = value;
	}

	@Override
	public void execute(State state) {
		state.CADD(value);
	}

	@Override
	public void genCode(MethodVisitor mv) {
		mv.visitVarInsn(ALOAD, 0);
		switch(value) {
			case 0:
				mv.visitInsn(ICONST_0);
				break;
			case 1:
				mv.visitInsn(ICONST_1);
				break;
			case 2:
				mv.visitInsn(ICONST_2);
				break;
			case 3:
				mv.visitInsn(ICONST_3);
				break;
			case 4:
				mv.visitInsn(ICONST_4);
				break;
			case 5:
				mv.visitInsn(ICONST_5);
				break;
			default:
				mv.visitIntInsn(BIPUSH, value);
				break;
		}
		mv.visitMethodInsn(INVOKEVIRTUAL, "bf/program/State", "CADD", "(I)V");
	}

}
